import { types } from "util";

(() => {

    const add = function (x, y) {
        return x + y
    }

    let sum = add(1, 2)
    console.log('sum:', sum)


    // ts 完整的函数写法
    const add2 = function (x: number, y: number): number {
        return x + y
    }

    sum = add2(3, 4)
    console.log('sum:', sum)

    // 剩余参数，超过一个的参数，都放在restOfName中，其实是一个数组
    function buildName(firstName: string, ...restOfName: string[]) {
        console.log(9527, restOfName.length);
        restOfName.forEach(element => {
            console.log(9527, element);
        });

        for (const name in restOfName) {
            console.log(9528, name);
        }

        for (const key in restOfName) {
            if (Object.prototype.hasOwnProperty.call(restOfName, key)) {
                const element = restOfName[key];
                console.log(9529, element);
            }
        }

        return firstName + " " + restOfName.join(" ");
    }

    let employeeName = buildName("Joseph", "Samuel", "Lucas", "MacKinzie");
    console.log('employeeName', employeeName)

    // 可选参数，用问号标示，需要注意的是，可选参数只能在必选参数之后
    function buildName2(firstName: string, midName: string, lastName?: string): string {
        if (lastName) {
            return firstName + '_' + midName + '_' + lastName
        }
        return firstName + '_' + midName
    }

    // 默认参数
    function buildName3(firstName: string = '古月', midName: string, lastName?: string): string {
        if (lastName) {
            return firstName + '_' + midName + '_' + lastName
        }
        return firstName + '_' + midName
    }


    // 函数重载：函数名
    function add3(x: string | number, y: string | number): number {
        if (typeof x === 'number' && typeof y === 'number')
            return x + y
        else if (typeof x === 'string' && typeof y === 'string')
            return (parseInt(x) + parseInt(y))
        return 0
    }

    let x = add3('4', '5')

    console.log(x)


    let name = buildName2('古月', '屠龙', '倚天')
    name = buildName2('古月', '屠龙')
    console.log(name)

    name = buildName3(undefined, '倚天') // 古月_屠龙
    name = buildName3(null, '倚天') // null_倚天
    name = buildName3('', '倚天') // _倚天
    console.log(name)


    // function func1(str: string): string {
    //     return str;
    // }

    // function func1(str: string, str2?: string) {
    //     return str + str2;
    // }

})()